/**
 * Created by duwei on 2018/6/4.
 */
var NewCoinShopCellItem = Item.extend({
    _ccsFileName: res.NewCoinShopCellUnit_json,
    addListener: function (code, listener) {
        network.addListener(code, listener, this);
    },


    /**
     * 设置数据
     * @param index
     * @param data
     * @param clubid
     */
    refreshData: function (index, data, limit_list, citype) {
        // cc.log(index, data);
        var self = this;
        if (data.tagtype != 'packet') {
            var iconSp = new cc.Sprite(self.getImageName(data));
            iconSp.setPosition(cc.p(self._nodeList['_root.bg'].getContentSize().width / 2, 355));
            self._nodeList['_root.bg'].addChild(iconSp);
        }
        else {
            self._nodeList['_root.gift'].setVisible(true);
            loadImageToSprite(self.getImageName(data), self._nodeList['_root.gift']);
        }
        self._nodeList['_root.itemName'].setString(data.name);
        self._nodeList['_root.itemdesc'].setString(data.desc);

        if (data.consume.code == 'diamond') {
            self._nodeList['_root.btn_buy.zuanshiSp'].setVisible(true);
            self._nodeList['_root.btn_buy.buyNum'].setString(data.consume.now_val);
        }
        else {
            var price = data.consume.now_val;
            price = price / 100;
            self._nodeList['_root.btn_buy.zuanshiSp'].setVisible(false);
            self._nodeList['_root.btn_buy.buyNum'].setString('￥' + price);
        }

        // var shunxuCfg=['tehui','jiazeng','xianshi','xiangou','rexiao'];
        //标签
        if (data.selltype && data.selltype.length > 0) {
            for (var i = 0; i < data.selltype.length; i++) {
                self._nodeList['_root'][data.selltype[i]].setVisible(true);
                self._nodeList['_root'][data.selltype[i]].setPositionY(459.79 - 65 * i);
            }
        }

        //限购次数
        var remainval = limit_list[data.pid];
        if (remainval > -1) {
            self._nodeList['_root.leftLb'].setVisible(true);
            self._nodeList['_root.leftLb'].setString('还可购' + remainval + '次');
        }
        else {
            self._nodeList['_root.leftLb'].setVisible(false);
        }

        //限时
        if (data.flashsale) {
            self._nodeList['_root.leftBg'].setVisible(true);
            self._nodeList['_root.leftTimeLb'].setVisible(true);
            var endTime = data.flashsale.etime
            var time = time2timestamp(endTime)
            self._nodeList['_root.leftTimeLb'].setString(timeStr2(time)+'过期');
        }
        else {
            self._nodeList['_root.leftBg'].setVisible(false);
            self._nodeList['_root.leftTimeLb'].setVisible(false);
        }

        TouchUtil.setOnclickListener(self._nodeList['_root.btn_buy'], function () {
            self._clickBuy(data.pid);
        });

        //购买订单
        network.addListener(1074, function (data) {
            cc.log('1074====' + JSON.stringify(data));
            hideLoading();
            if (data.errorCode == 0) {
                if (data.prepayid != '') {
                    var openUrl = NewCoinShopCfg.NewCoinShopPayCoinUrl + 'area=' + app.area + '&buyerId=' + gameData.uid + '&source=' + NewCoinShopCfg.NewCoinShopCoinSource + '&ciType=' + citype + '&payType=6' + '&signKey=' + Crypto.MD5("feiyu-pay" + gameData.uid + app.area) + '&prepayid=' + data.prepayid + '&#http://';
                    cc.log('openUrlopenUrl==' + openUrl);
                    cc.sys.openURL(openUrl);

                }
                else {
                    showAlert('支付成功');
                    network.send(MsgCode.ReqFangKa, {});
                }

            }
            else {
                showAlert(data.errorMsg);
            }

        });


    },
    getImageName: function (data) {
        var self = this;
        self._nodeList['_root.bg'];
        var nameStr = null;
        var imageIndex = 1;
        if (data.tagtype == 'coin') {
            if (data.content[0].orig_val <= 100000) {
                imageIndex = 1
            }
            else if (data.content[0].orig_val <= 1000000) {
                imageIndex = 2
            }
            else if (data.content[0].orig_val <= 2000000) {
                imageIndex = 3
            }
            else {
                imageIndex = 4
            }
            nameStr = 'modules/main/activity/res/NewCoinShopWindow/gold_' + imageIndex + '.png';
        }
        else if (data.tagtype == 'diamond') {
            if (data.content[0].orig_val <= 500) {
                imageIndex = 1
            }
            else if (data.content[0].orig_val <= 2000) {
                imageIndex = 2
            }
            else if (data.content[0].orig_val <= 10000) {
                imageIndex = 3
            }
            else {
                imageIndex = 4
            }
            nameStr = 'modules/main/activity/res/NewCoinShopWindow/zuanshi_' + imageIndex + '.png';
        }
        else {
            nameStr = data.url;
        }
        return nameStr;
    },
    _clickBuy: function (pid) {
        //获取订单中
        showLoading('获取订单中....');
        network.send(1073,
            {
                userid: gameData.uid + '',
                areaid: NewCoinShopCfg.gameid,
                pid: pid,
                appid: app.appId + '',
                gameid: 'mall',
                positionid: 0
            }
        );

        // showMessageBox('您是否购买该商品?', function () {
        //     //获取订单中
        //     showLoading('获取订单中....');
        //     network.send(1073,
        //         {
        //             userid: gameData.uid + '',
        //             areaid: NewCoinShopCfg.gameid,
        //             pid: pid,
        //             appid: app.appId + '',
        //             gameid: 'mall',
        //             positionid: 0
        //         }
        //     );
        // })


    },
});